/*
 * xAPdevBSCtable.java
 *
 * Java class used to store data of multiple xAP devices.
 * A dynamically sizable vector is used so that new endpoints can be added
 * anytime.
 *
 * Copyright (c) 2009 Daniel Berenguer <dberenguer@usapiens.com>
 *
 * This file is part of the jxAP project.
 *
 * OPNODE is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * OPNODE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with canTracer; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * 29/10/09 by Daniel Berenguer : first version
 */

package xAP;

import java.util.Vector;

/********************************************************************************************
 * xAPdevBSCtable class
 *
 * Class used to store xAP data
 *
 *********************************************************************************************/
public class xAPdevBSCtable
{
   /********************************************************************************************
    * Member variables
    ********************************************************************************************/
    // Vector of devices
    private Vector vTable;

   /********************************************************************************************
    * xAPdevBSCtable
    *
    * Class constructor
    *
    ********************************************************************************************/
    public xAPdevBSCtable()
    {
        // Create vector of devices
        vTable = new Vector();
    }

   /********************************************************************************************
    * updateTableFromMessage
    *
    * Update endpoint in the vector or add new endpoint or device if it doesn't exists from the
    * xAP message passed as argument
    *
    * 'message'    Message to extract data from
    *
    * Returns:
    *   true if the device has changed
    *   false if the device has not changed
    ********************************************************************************************/
    public boolean updateTableFromMessage(xAPmessage message)
    {
        xAPdevice device = new xAPdevice(message);

        return updateDevice(device);
    }

   /********************************************************************************************
    * updateDevice
    *
    * Update device in the vector or add new device if it doesn't exists
    *
    * 'device'    Device to update
    *
    * Returns:
    *   true if the device has changed
    *   false if the device has not changed
    ********************************************************************************************/
    public boolean updateDevice(xAPdevice device)
    {
        // Index of endpoint in table
        int index = indexOfDeviceFromUID(device.uid);

        // New device?
        if (index == -1)
        {
            // Add endpoint to the table
            addDevice(device);
        }
        else    // The device already exists in the vector
        {
            xAPdevice devElement = (xAPdevice)vTable.elementAt(index);

            // Update device in table only if it's changed
            if (devElement.equals(device) == false)
            {
                vTable.setElementAt(device, index);
                return true;
            }
        }

        return false;
    }

   /********************************************************************************************
    * indexOfDeviceFromUID
    *
    * Search the index of the device given its xAP UID
    *
    * 'UID'    UID to search
    *
    * Returns:
    *   index into the vector
    *   -1 if the device is not found
    ********************************************************************************************/
    private int indexOfDeviceFromUID(String UID)
    {
        xAPdevice devElement;
        int i;

        for(i=0 ; i<vTable.size() ; i++)
        {
            devElement = (xAPdevice)vTable.elementAt(i);

            if (devElement.uid.compareToIgnoreCase(UID) == 0)
                return i;
        }

        return -1;
    }

   /********************************************************************************************
    * getUIDfromSource
    *
    * Search the xAP UID of a device given its Source adddress
    *
    * 'Source'    Source address to search
    *
    * Returns:
    *   UID string
    ********************************************************************************************/
    public String getUIDfromSource(String Source)
    {
        xAPdevice devElement;
        int i;

        for(i=0 ; i<vTable.size() ; i++)
        {
            devElement = (xAPdevice)vTable.elementAt(i);

            if (devElement.source.compareToIgnoreCase(Source) == 0)
                return devElement.uid;
        }

        return null;
    }

   /********************************************************************************************
    * length
    *
    * Length of the table
    *
    * Returns:
    *   table length
    ********************************************************************************************/
    public int length()
    {
        return vTable.size();
    }

   /********************************************************************************************
    * deviceAt
    *
    * Return device at a given location
    *
    * 'index'       index of the device within the table
    *
    * Returns:
    *   device
    ********************************************************************************************/
    public xAPdevice deviceAt(int index)
    {
        return (xAPdevice)vTable.elementAt(index);
    }

   /********************************************************************************************
    * addDevice
    *
    * Add new device at the end of the vector
    *
    * 'device'    Device to add
    *
    ********************************************************************************************/
    private void addDevice(xAPdevice device)
    {
        vTable.addElement(device);
    }
}
